package app.view;

import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.JTextField;

public class DataCellEditor extends javax.swing.AbstractCellEditor implements
		javax.swing.table.TableCellEditor {

	private static final long serialVersionUID = 1L;
	// This is the component that will handle the editing of the cell value
	private JComponent component = new JTextField();

	private ArrayList<EditingFinishedListener> listeners = new ArrayList<EditingFinishedListener>();

	public void addListener(EditingFinishedListener efl) {
		listeners.add(efl);
	}
	
	public JTextField getTextField() {
		return (JTextField) component;
	}

	public DataCellEditor() {
		super();

		((JTextField) component).addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent arg0) {
			}

			@Override
			public void focusGained(FocusEvent arg0) {
				((JTextField) component).selectAll();
			}
		});
		
		((JTextField) component).addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent arg0) {

				String texto = ((JTextField) component).getText();

				if (contaBarras(texto) == 2 && texto.split("/").length == 3
						&& texto.split("/")[2].length() == 3) {
					
					((JTextField) component).setText(texto + arg0.getKeyChar());
					
					stopCellEditing();
					for (EditingFinishedListener efl : listeners)
						efl.trigger();
					
					arg0.consume();
				}
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
			}

			@Override
			public void keyPressed(KeyEvent arg0) {

				if (arg0.getKeyCode() == KeyEvent.VK_BACK_SPACE)
					return;

				String texto = ((JTextField) component).getText();

				if (texto.length() == 2 && contaBarras(texto) == 0)
					((JTextField) component).setText(((JTextField) component)
							.getText() + "/");

				else if (texto.length() == 5 && contaBarras(texto) == 1)
					((JTextField) component).setText(((JTextField) component)
							.getText() + "/");

			}

			private int contaBarras(String texto) {

				int counter = 0;

				for (char c : texto.toCharArray())
					if (c == '/')
						counter++;

				return counter;
			}
		});
	}

	// This method is called when a cell value is edited by the user.
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int rowIndex, int vColIndex) {
		// 'value' is value contained in the cell located at (rowIndex,
		// vColIndex)

		if (isSelected) {
			// cell (and perhaps other cells) are selected
		}

		// Configure the component with the specified value
		((JTextField) component).setText((String) value);

		// Return the configured component
		return component;
	}

	// This method is called when editing is completed.
	// It must return the new value to be stored in the cell.
	public Object getCellEditorValue() {
		return ((JTextField) component).getText();
	}

}
